package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kotlinx.android.synthetic.main.activity_machine_back.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.*


class MachineBackActivity : BaseActivity(), IView {


    private var member_id = ""
    private var member_mobile = ""
    private var produce_id = ""
    private var sn_code = "" // 调拨sn_code


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.tv_back_name -> Intent(this, SelectTransferPeopleActivity::class.java).run {
                startActivityForResult(this, 100)
            }
            R.id.tv_produce -> {
                if (member_id.isEmpty()) {
                    showToast("选择回拨的代理商")
                    return@OnClickListener
                }
                Intent(this, SelectProduceActivity::class.java).run {
                    putExtra("member_id", member_id)
                    putExtra("type", 1)
                    putExtra("gc_id",intent.getStringExtra("gc_id"))
                    startActivityForResult(this, 100)
                }
            }
            R.id.tv_sn_number -> {
                if (produce_id.isEmpty()) {
                    showToast("选择回拨机具型号")
                    return@OnClickListener
                }
                Intent(this, SelectSnCodeActivity::class.java).run {
                    putExtra("goods_id", produce_id)
                    putExtra("type","backallow")
                    putExtra("member_id",member_id)
                    startActivityForResult(this, 100)
                }
            }

            R.id.tv_apply -> submit()
//                R.id. -> startActivity(ForgetPasswordActivity::class.java)
        }
    }

    private fun submit() {


        if (TextUtils.isEmpty(produce_id)) {
            showToast("请先选择调拨产品")
            return
        }

        if (TextUtils.isEmpty(sn_code)) {
            showToast("请先选择SN号")
            return
        }

        val name = tv_back_name.text.toString()

        val number = tv_shuliang.text.toString()
        if (TextUtils.isEmpty(name)) {
            showToast("请先输入伙伴姓名")
            return
        }
        if (TextUtils.isEmpty(member_mobile)) {
            showToast("请先输入伙伴手机号")
            return
        }

        val params = HashMap<String, String>()
        params["key"] = token
        params["goods_id"] = produce_id
        params["sn_code"] = sn_code
        params["member_name"] = name
        params["member_mobile"] = member_mobile
        params["num"] = number
        params["client"] = "android"
        RetrofitHelper.service.initiateATransferBack(params).sss(this)
        {
            showToast(it.result.toString())
            finish()
        }

    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_machine_back
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "未激活机具回拨"
        iv_back.setOnClickListener { finish() }
        tv_back_name.setOnClickListener(onClickListener)
        tv_produce.setOnClickListener(onClickListener)
        tv_sn_number.setOnClickListener(onClickListener)
        tv_apply.setOnClickListener(onClickListener)

    }

    override fun start() {

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            200 -> {
                produce_id = ""
                tv_produce.text = ""
                tv_sn_number.text = ""
                tv_shuliang.text = ""
                member_id = data?.getStringExtra("member_id").toString()
                tv_back_name.text = data?.getStringExtra("member_name").toString()
                member_mobile = data?.getStringExtra("member_mobile").toString()
            }
            300 -> {
                tv_sn_number.text = ""
                tv_shuliang.text = ""
                produce_id = data?.getStringExtra("produce_id").toString()
                tv_produce.text = data?.getStringExtra("produce_name").toString()
            }

            400 -> {
                tv_shuliang.text = data?.getStringExtra("number").toString()
                tv_sn_number.text = data?.getStringExtra("sn_show").toString()
                sn_code = data?.getStringExtra("sn_code").toString()
            }
        }
    }

}
